package com.iqiyi.controller;

import com.iqiyi.dto.ActionResult;
import com.iqiyi.dto.Usersa;
import com.iqiyi.po.IqiyiUser;
import com.iqiyi.service.IqiyiService;
import com.iqiyi.service.IqiyiUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.iqiyi.dto.ActionResult;
import com.iqiyi.dto.LoginInfo;
import com.iqiyi.po.IqiyiUser;
import com.iqiyi.service.IqiyiUserService;
import com.iqiyi.util.QiniuUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

/**
 * 爱奇艺用户的控制层
 *
 * @author YoungKai
 * @date 2021/2/24 11:23
 */
@RestController
@RequestMapping("/iqiyiUser")
public class IqiyiUserController {

    @Autowired(required = false)
    private IqiyiUserService iqiyiUserService;

    @Autowired
    private IqiyiService iqiyiService;

    @RequestMapping(value = "/userByUid",method = RequestMethod.GET)
    public ActionResult userByUidUser(Integer uid){
        IqiyiUser iqiyiUser = iqiyiUserService.selectByUid(uid);
        if (iqiyiUser!=null){
            return new ActionResult(200," successful",iqiyiUser);
        }else {
            return new ActionResult(400," fail",null);
        }
    }




    /**
     * 根据手机号查询是否存在当前用户
     */
    @RequestMapping(value = "/findUserByUphone",method = RequestMethod.GET)
    public ActionResult findUserByUphone(String uphone){
        System.out.println(uphone);
        List<IqiyiUser> iqiyiUsers = iqiyiUserService.selectIqiyiUsetByUphone(uphone);
        if(iqiyiUsers != null && iqiyiUsers.size() >0){
            System.out.println(iqiyiUsers.size());
            return new ActionResult(200,"select success",iqiyiUsers);
        }else{
            return new ActionResult(404,"select null",null);
        }
    }

    /**
     * 注册用户
     * @param iqiyiUser
     * @return
     */
    @RequestMapping(value = "/registUser",method = RequestMethod.POST)
    public ActionResult registUser(@RequestBody IqiyiUser iqiyiUser){
        int i = iqiyiUserService.insertIqiyiUSerByUphone(iqiyiUser);
        if(i > 0){
            return new ActionResult(200,"注册成功",iqiyiUser);
        }
        return new ActionResult(500,"注册失败",iqiyiUser);
    }

    /**
     * 用户登录
     * @param
     * @return
     */
    @RequestMapping(value = "/loginUser",method = RequestMethod.POST)
    public ActionResult loginUser(@RequestBody LoginInfo loginInfo){
        List<IqiyiUser> iqiyiUsers = iqiyiUserService.selectIqiyiUserByUphoneAndPassWord(loginInfo);
        if(iqiyiUsers == null || iqiyiUsers.size() == 0 ){
            return new ActionResult(404,"账号或密码错误",null);
        }
        return new ActionResult(200,"登录成功",iqiyiUsers.get(0));
    }

    /**
     * 用户退出
     * @return
     */
    @RequestMapping(value = "/exitLogin",method = RequestMethod.GET)
    public ActionResult exitLogin(){
        iqiyiUserService.exitLoginUser();
        return new ActionResult(200,"用户退出成功",null);
    }

    /**
     * 在redis中获取登录用户信息
     */
    @RequestMapping(value = "getUserInRedis",method = RequestMethod.GET)
    public ActionResult getUserInRedis(){
        IqiyiUser userInRedis = iqiyiUserService.getUserInRedis();
        if(userInRedis != null) {
            return new ActionResult(200, "用户已登录", userInRedis);
        }else{
            return new ActionResult(500,"",null);
        }
    }


    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public String upload(@RequestParam("file") MultipartFile file){
        String name = UUID.randomUUID().toString();
        try {
            String filename = QiniuUpload.updateFile(file,name);
            return filename;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    @RequestMapping(value = "/insertUser" , method = RequestMethod.POST)
    public int insertUser(@RequestBody IqiyiUser iqiyiUser){
        return iqiyiService.insertUser(iqiyiUser);

    }
    @RequestMapping(value = "/updateByUser" , method = RequestMethod.POST)
    public ActionResult updateByUser(@RequestBody Usersa iqiyiUser){


            int i = iqiyiService.updateByUser(iqiyiUser);
            if (i>-1){
                return new ActionResult(200, "修改成功!!!", null);
            }


            return new ActionResult(204, "没有找到修改内容!!!", null );

    }

    @RequestMapping(value = "/userByuid",method = RequestMethod.GET)
    public ActionResult userByuidShow(Integer uid){
        IqiyiUser byiqiyiUser = iqiyiService.findByiqiyiUser(uid);
        return new ActionResult(200,"successful",byiqiyiUser);
    }
    @RequestMapping(value = "/findAllUser" , method = RequestMethod.GET)
    public ActionResult findAllVideo(IqiyiUser iqiyiUser){
        List<IqiyiUser> videos = iqiyiService.findAllUser(iqiyiUser);
        if (videos!=null){
            return new ActionResult(200,"查询成功",videos);
        }
        return new ActionResult(201,"查询失败",null);
    }
}
